package test3_25.demo08;

public class Homework14_6_SameString {
    public static void main(String[] args) {
        String str1 = "abcwerthelloyuiodef";
        String str2 = "cvhellobnm";
        String result = getMaxCommonSubstring(str1, str2);
        System.out.println("最大相同子串: " + result);
    }

    public static String getMaxCommonSubstring(String str1, String str2) {
        String shortStr;
        String longStr;
        if (str1.length() <= str2.length()) {
            shortStr = str1;
            longStr = str2;
        } else {
            shortStr = str2;
            longStr = str1;
        }
        for (int len = shortStr.length(); len > 0; len--) {
            for (int i = 0; i + len <= shortStr.length(); i++) {
                String substring = shortStr.substring(i, i + len);
                if (longStr.contains(substring)) {
                    return substring;
                }
            }
        }
        return "";
    }
}
